package main

import (
	"fmt"
)

/*
给定一个整数数组 nums 和一个目标值 target，请你在该数组中找出和为目标值的那 两个 整数，并返回他们的数组下标。

你可以假设每种输入只会对应一个答案。但是，你不能重复利用这个数组中同样的元素。

示例:

给定 nums = [2, 7, 11, 15], target = 9

因为 nums[0] + nums[1] = 2 + 7 = 9
所以返回 [0, 1]

*/
func twoSum(nums []int, target int) []int {
	small := []int{}
	large := []int{}
	center := []int{}
	for _, item := range nums {
		if item < target/2 {
			small = append(small, item)
		} else if item > target/2 {
			large = append(large, item)
		} else {
			center = append(center, item)
		}
	}
	flag := false
	flag1, flag2 := 0, 0
	if len(center) > 1 {
		if center[0]+center[1] == target {
			flag = true
			flag1, flag2 = center[0], center[1]
		}
	} else if len(center) == 1 && target > 0 {
		small = append(small, center[0])
	} else if len(center) == 1 && target < 0 {
		large = append(large, center[0])
	}
	result := []int{0, 0}
	if flag == false {
		for _, a := range small {
			for _, b := range large {
				if a+b == target {
					flag1 = a
					flag2 = b
					flag = true
					break
				}
			}
			if flag == true {
				break
			}
		}
	}
	if flag == true {
		for s, i := range nums {
			if i == flag1 {
				result[0] = s
				break
			}

		}
		for s, i := range nums {
			if i == flag2 && result[0] != s {
				result[1] = s
				break
			}
		}
	}
	return result
}

func main() {
	nums := []int{0, 3, -3, 4, -1}
	target := -1
	result := twoSum(nums, target)
	fmt.Println(result)
	if result[0] != 2 && result[1] != 1 {
		fmt.Println("false")
		return
	}
	fmt.Println("true")
}
